package com.ccl.iot.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.ccl.iot.R;
import com.ccl.iot.publics.UIUtils;


public class DeviceItemView extends RelativeLayout {

    private static int LONG_PRESSS_ROUND = UIUtils.dp2px(15);

    private float mDownX;
    private float mDewnY;

    public DeviceItemView(Context context) {
        this(context, null);
    }

    public DeviceItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DeviceItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater from = LayoutInflater.from(context);
        from.inflate(R.layout.device_item, this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        requestDisallowInterceptTouchEvent(true);
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDewnY = ev.getY();
//                Log.e("MotionEvent", "dispatchTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
//                Log.e("MotionEvent", "dispatchTouchEvent ACTION_MOVE");
                float moveX = ev.getX();
                float moveY = ev.getY();
                if(Math.abs(moveX - mDownX) > LONG_PRESSS_ROUND){
//                    Log.e("MotionEvent", "dispatchTouchEvent ACTION_MOVE X");
                    requestDisallowInterceptTouchEvent(false);
                    return false;
                }

                if(Math.abs(moveY - mDewnY) > LONG_PRESSS_ROUND){
//                    Log.e("MotionEvent", "dispatchTouchEvent ACTION_MOVE Y");
                    requestDisallowInterceptTouchEvent(false);
                    return false;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
//                Log.e("MotionEvent", "dispatchTouchEvent ACTION_UP");
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

}
